const fetch = require("nb-fetch/fetch")
const db = require('../libs/db')
const vdjs = require('vdjs')
const Config = db.model('config')
const Log = db.model('log')


exports.config = async ctx => {
  var {keys} = ctx.query
  var where = {}
  if (keys) {
    where.key = {
      $in: keys.split(',')
    }
  }
  var config = await Config.findAll({where})

  ctx.body = {
    bool: true,
    msg: '',
    result: config
  }
}

exports.updateKey = async ctx => {
  var valid = vdjs.validate(ctx.request.body, {
    // id: {required: true, not: '', msg: 'id不能为空'},
    key: {required: true, not: '', msg: '必须指定键名'},
    value: {required: true, msg: '必须提供键值'},
    description: {not: '', msg: '键描述不能为空'}
  })
  if (valid.fails()) {
    return ctx.body = {
      bool: false,
      msg: valid.first(),
      valid: valid.all()
    }
  }description
  var {key, value, description} = ctx.request.body
  
  try {
    var rows = await Config.update({value, description}, {where: {key}})
  } catch (err) {
    return ctx.body = {
      bool: false,
      msg: '修改失败',
      err
    }
  }
  var oper = await ctx.user()
  await Log.create({
    uid: oper.id,
    ip: ctx.ip,
    action: 'config',
    result: 'true',
    desctiption: `修改配置 ${key} 为 ${value}`
  })
  // 刷新配置缓存
  try {
    var url = process.env.REFRESH_CONFIG_URL
    if (url) {
      await fetch.get(url, {
        type: 'text',
        params: makeSign()
      })
    }
  } catch (err) {
    log.error(err)
  }
  return ctx.body = {
    bool: true,
    msg: '修改成功'
  }
}

exports.updateConfig = async ctx => {
  var oper = await ctx.user()
  if (oper.type !== 1000) {
    ctx.body = {
      bool: false,
      msg: '您无权操作'
    }
    return false
  }
  var {
    id,
    version,
    shareUrl,
    appId,
    appSecret,
    defaultGems,
    privateKey,
    defaultCountdown,
    default10JuCreatorRate,
    default10JuAARate,
    default20JuCreatorRate,
    default20JuAARate,
    default30JuCreatorRate,
    default30JuAARate,
    defaultShareGetCards,
    defaultBindAgentGift
  } = ctx.request.body
  if (!id) {
    ctx.body = {
      bool: false,
      msg: 'id不能为空'
    }
    return false
  }
  var data = {
    version,
    shareUrl,
    appId,
    appSecret,
    defaultGems,
    privateKey,
    defaultCountdown,
    default10JuCreatorRate,
    default10JuAARate,
    default20JuCreatorRate,
    default20JuAARate,
    default30JuCreatorRate,
    default30JuAARate,
    defaultShareGetCards,
    defaultBindAgentGift
  }
  var count = 0
  for (var k in data) {
    if (typeof data[k] === 'undefined') {
      delete data[k]
    } else {
      count++
    }
  }
  if (count > 0) {
    try {
      await Config.update(data, {where: {id}})
    } catch (err) {
      log(err)
      ctx.body = {
        bool: false,
        msg: '操作失败',
        err
      }
      return false
    }
  }
  await Log.create({
    uid: oper.id,
    ip: ctx.ip,
    action: 'config',
    result: 'true',
    desctiption: '修改了配置'
  })
  ctx.body = {
    bool: true,
    msg: '修改成功'
  }

}
